package com.cg.controller;

import com.cg.entity.Category;
import com.cg.entity.User;
import com.cg.service.CategoryService;
import com.cg.service.EventsService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Controller
@RequestMapping("/user")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private EventsService eventsService;
    @RequestMapping(value = "/category")
    public String category(HttpServletRequest request,Model model){

        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            Integer userId= user.getUserId();
            List<Category>categoryList= categoryService.categoryList(userId,"");
            request.getSession().setAttribute("categoryList", categoryList);
            return "category";
        }else {
            model.addAttribute("MainError", "请先登录");
            return "login";
        }
    }
    @RequestMapping(value = "/addcategory")
    public String addcategory(@RequestParam("categoryName") String categoryName, HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        Category one=categoryService.findCategoryByName(categoryName,userId);
        if(one==null){
            categoryService.addcategory(categoryName, userId);
            return "redirect:/user/category";
        }else {
            model.addAttribute("categoryError", "分类名已存在");
            return "category";
        }
    }
    @RequestMapping(value = "/editCategory")
    public String editcategory(@RequestParam("categoryId") Integer categoryId,HttpServletRequest request) {
        Category category = categoryService.findCategoryById(categoryId);
       request.getSession().setAttribute("category", category);
            return "editCategory";
    }
    @RequestMapping(value = "/doEditCategory")
    public String doEditCategory(Category category,Model model,HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        // 首先，根据categoryId查询原始分类名称
        Category one = categoryService.findCategoryByName(category.getCategoryName(),userId);
        if (one == null ) {
            categoryService.editcategory(category);
        }else{
            model.addAttribute("categoryError", "分类名已存在");
            return "category";
        }
        // 重定向到分类列表页面
        return "redirect:/user/category";
    }

    @RequestMapping(value = "/deletecategory")
    public String deletecategory(@RequestParam("categoryId") Integer categoryId, Model model) {
         int num = eventsService.findByCategoryId(categoryId);
        if (num == 0) {
            categoryService.deletecategory(categoryId);
        }else {
            model.addAttribute("categoryError", "该分类名下有事件，请先删除事件后再删除");
            return "category";
        }
        // 重定向到分类列表页面
        return "redirect:/user/category";
    }
    @RequestMapping(value = "/doSearch")
    public String search(@RequestParam("searchName") String searchName,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Integer userId= user.getUserId();
        List<Category>categoryList = categoryService.categoryList(userId,searchName);
        request.getSession().setAttribute("categoryList", categoryList);
        return "category";
    }
}
